<?
ob_start("ob_gzhandler");
require "include/bittorrent.php";
dbconn(false);
loggedinorreturn();

$cats = genrelist(2);

$searchstr = unesc($_GET["search"]);
$cleansearchstr = searchfield($searchstr);
if (empty($cleansearchstr))
unset($cleansearchstr);

if ($_GET['sort'] && $_GET['type']) {

$column = '';
$ascdesc = '';

switch($_GET['sort']) {
case '1': $column = "name"; break;
case '2': $column = "numfiles"; break;
case '3': $column = "comments"; break;
case '4': $column = "added"; break;
case '5': $column = "size"; break;
case '6': $column = "times_completed"; break;
case '7': $column = "seeders"; break;
case '8': $column = "leechers"; break;
case '9': $column = "owner"; break;
default: $column = "id"; break;
}

switch($_GET['type']) {
case 'asc': $ascdesc = "ASC"; $linkascdesc = "asc"; break;
case 'desc': $ascdesc = "DESC"; $linkascdesc = "desc"; break;
default: $ascdesc = "DESC"; $linkascdesc = "desc"; break;
}


$orderby = "ORDER BY torrents." . $column . " " . $ascdesc;
$pagerlink = "sort=" . intval($_GET['sort']) . "&type=" . $linkascdesc . "&";

} else {

$orderby = "ORDER BY torrents.id DESC";
$pagerlink = "";

}

$addparam = "";
$wherecatina = array();

// get torrents of only one category section
$where = 'section = 2';

if (!isset($_GET["incldead"]) && get_user_class() >= UC_MODERATOR)
{
	if (!isset($CURUSER) || get_user_class() < UC_ADMINISTRATOR)
		$where .= " AND banned != 'yes'";
}
elseif ($_GET["incldead"] == 1)
{
	$addparam .= "incldead=1&amp;";
	if (!isset($CURUSER) || get_user_class() < UC_ADMINISTRATOR)
		$where .= " AND banned != 'yes'";
}
elseif ($_GET["incldead"] == 2)
{
	$addparam .= "incldead=2&amp;";
		$where .= " AND visible = 'no'";
}
else
	$where .= " AND visible = 'yes'";

if ($CURUSER["hide_xxx"] == "yes")
 $where .= ($where ? " AND " : "") . "category != '10'";

$wherebase = $where;

$all = $_GET["all"];

if (!$all)
	if (!$_GET && $CURUSER["notifs"])
	{
	  $all = True;
	  foreach ($cats as $cat)
	  {
	    $all &= $cat[id];
	    if (strpos($CURUSER["notifs"], "[cat" . $cat[id] . "]") !== False)
	    {
	      $wherecatina[] = $cat[id];
	      $addparam .= "cat$cat[id]=1&amp;";
	    }
	  }
	}
	else
	{
	  $all = True;
	  foreach ($cats as $cat)
	  {
	    $all &= $_GET["cat$cat[id]"];
	    if ($_GET["cat$cat[id]"])
	    {
	      $wherecatina[] = $cat[id];
	      $addparam .= "cat$cat[id]=1&amp;";
	    }
	  }
	}
if ($all)
{
$wherecatina = array();
$addparam = "";
}

if (isset($cleansearchstr))
{
	$where .= ($where ? " AND " : "") . "torrents.name LIKE (" . sqlesc($searchstr) . ")";
	$addparam .= "search=" . urlencode($searchstr) . "&amp;";
}

if (count($wherecatina) > 1)
{
	$wherecatin = implode(",",$wherecatina);
	$wherebase2 = "category IN(" . $wherecatin . ")";
	$where .= ($where ? " AND " : "") . "category IN(" . $wherecatin . ")";
}
elseif (count($wherecatina) == 1)
{
	$wherebase2 = "category = $wherecatina[0]";
	$where .= ($where ? " AND " : "") . "category = $wherecatina[0]";
}

if ($where != "")
	$where = "WHERE $where";

$res = query("SELECT COUNT(1) FROM torrents LEFT JOIN categories ON category = categories.id $where") or die(mysql_error());
$row = mysql_fetch_array($res);
$count = $row[0];

if (!$count && isset($cleansearchstr)) {
	$where = $wherebase;
	$searcha = explode(" ", $cleansearchstr);
	$sc = 0;
	foreach ($searcha as $searchss) {
		if (strlen($searchss) < 1)
			continue;
		$sc++;
		if ($sc > 5)
			break;
		$ssa = array();
		foreach (array("torrents.name") as $sss)
         	   $ssa[] = "$sss LIKE '%" . sqlwildcardesc($searchss) . "%'";
          	  $where .= ($where ? " AND " : "") . "(" . implode(" OR ", $ssa) . ")";
	}
	if ($sc) {
		if ($wherebase2)
		$where .= ($where ? " AND " : "") . $wherebase2;
		if ($where != "")
			$where = "WHERE $where";
		$res = query("SELECT COUNT(1) FROM torrents LEFT JOIN categories ON category = categories.id $where");
		$row = mysql_fetch_array($res);
		$count = $row[0];
	}
}

$torrentsperpage = $CURUSER["torrentsperpage"];
if (!$torrentsperpage)
	$torrentsperpage = 20;

if ($count)
{
    if ($addparam != "") {
 if ($pagerlink != "") {
  if ($addparam{strlen($addparam)-1} != ";") { // & = &amp;
    $addparam = $addparam . "&" . $pagerlink;
  } else {
    $addparam = $addparam . $pagerlink;
  }
 }
    } else {
 $addparam = $pagerlink;
    }
	list($pagertop, $pagerbottom, $limit) = pager($torrentsperpage, $count, "browse2.php?" . $addparam);
	$query = "SELECT torrents.id, torrents.category, torrents.leechers, torrents.seeders, torrents.name, torrents.times_completed, torrents.size, torrents.added, torrents.comments,torrents.numfiles,torrents.filename,torrents.owner,IF(torrents.nfo <> '', 1, 0) as nfoav," .
//	"IF(torrents.numratings < $minvotes, NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating, categories.name AS cat_name, categories.image AS cat_pic, users.username FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id $where $orderby $limit";
	"categories.name AS cat_name, categories.image AS cat_pic, users.username FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id $where $orderby $limit";
	$res = query($query) or die(mysql_error());
}
else
	unset($res);
if (isset($cleansearchstr))
	stdhead("Search results for \"$searchstr\"");
else
	stdhead("Archive");
?>

<STYLE TYPE="text/css" MEDIA=screen>

  a.catlink:link, a.catlink:visited{
		text-decoration: none;
	}

</STYLE>

<div style="width:750px; text-align:center; font-size:larger; font-weight:bold; border-style:solid; border-color:green">All torrents in this section are FREELEECH</div>
<br />

<form method="get" action="browse2.php">
<table class=bottom>
<tr>

<?
$i = 0;
foreach ($cats as $cat)
{
	$catsperrow = 6;
	print(($i && $i % $catsperrow == 0) ? "</tr><tr>" : "");
	print("<td class=bottom style=\"padding-bottom: 2px;padding-left: 7px\"><input name=cat$cat[id] type=\"checkbox\" " . 	(in_array($cat[id],$wherecatina) ? "checked " : "") . "value=1><a class=catlink href=browse2.php?cat$cat[id]=1>" . 	htmlspecialchars($cat[name]) . "</a></td>\n");
	$i++;
}

$alllink = "<div align=left>(<a href=browse2.php?all=1><b>Show all</b></a>)</div>";

$ncats = count($cats);
$nrows = ceil($ncats/$catsperrow);
$lastrowcols = $ncats % $catsperrow;

if ($lastrowcols != 0)
{
	if ($catsperrow - $lastrowcols != 1)
	{
		print("<td class=bottom rowspan=" . ($catsperrow  - $lastrowcols - 1) . ">&nbsp;</td>");
	}
	print("<td class=bottom style=\"padding-left: 5px\">$alllink</td>\n");
}
?>

</tr>
</table>
<p align="center">
Search:
<input type="text" name="search" size="45" value="<?= htmlspecialchars($searchstr) ?>" />
<select name=incldead>
<option value="0">Active</option>
<option value="1"<? print((($_GET["incldead"] == 1) || (!isset($_GET["incldead"]) && get_user_class() >= UC_MODERATOR)) ? " selected" : ""); ?>>All</option>
<option value="2"<? print($_GET["incldead"] == 2 ? " selected" : ""); ?>>Dead</option>
</select>
<input type="submit" class=btn value="Go!" />
</p>
</form>

<?
if (isset($cleansearchstr))
	print("<h2>Search results for \"" . htmlspecialchars($searchstr) . "\"</h2>\n");

if ($count) 
{
	print($pagertop);
	torrenttable($res);
	print($pagerbottom);
}
else 
{
	if (isset($cleansearchstr)) 
	{
		print("<h2>Nothing found!</h2>\n");
		print("<p>Try again with a refined search string.</p>\n");
	}
	else 
	{
		print("<h2>Nothing here!</h2>\n");
		print("<p>Sorry pal :(</p>\n");
	}
}

query("UPDATE users SET last_browse = NOW() where id = " . $CURUSER['id']);

stdfoot();
?>